import 'package:enterprise_v6_app/app/components/common_selection/common_selection_view.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_accident_report_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:get/get.dart';

class AccidentResubmitController extends GetxController {
  /// 需要提交的实体类
  EmergencyAccidentReportEntity entity = EmergencyAccidentReportEntity();

  /// 处置状态列表
  List<GetDictAllEntity> disposalStatusList = [];

  /// 选中的事故事件
  EmergencyEventListEntity emergencyBean = EmergencyEventListEntity();

  /// 选中的处置状态
  GetDictAllEntity selectStatus = GetDictAllEntity();

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取处置状态列表
    disposalStatusList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'czzt_Kl6aH3Fk');
  }

  /// 事故选择
  void showEmergency() {
    Get.to(CommonSelectionPage<EmergencyEventListEntity>(title: '危险化学品选择', enableLoad: true), arguments: {
      'interfaceUrl': Interface.getAccidentInfoPageList,
      'searchName': 'accidentName',
    })?.then((value) {
      //如果不为空则设置信息
      if (value != null) {
        emergencyBean = value[0] as EmergencyEventListEntity;
        entity.accidentNameId = emergencyBean.getId();
        update();
      }
    });
  }

  ///处置状态选择
  void showDisposalStatus() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '处置状态选择',
      dataList: disposalStatusList,
      isMultiple: false,
      checkedList: [selectStatus],
      onCallBack: (value) {
        selectStatus = value[0];
        entity.handleState = selectStatus.getId();
        entity.handleStateLabel = selectStatus.getShowName();
        update();
      },
    );
  }

  /// 提交
  Future<void> submit() async {
    if (entity.accidentNameId.isEmpty) {
      AppToast.showWarning('请选择事故事件!');
      return;
    }
    if (entity.accidentUpdate.isEmpty) {
      AppToast.showWarning('请输入事故最新情况!');
      return;
    }
    if (entity.handleStateLabel.isEmpty) {
      AppToast.showWarning('请选择处置状态!');
      return;
    }
    final res = await HttpUtil().generatePostRequest(Interface.addOrUpdateReport, data: [entity.toJson()]).execute();
    res.onSuccess((result) {
      Get.back();
      AppToast.showWarning('提交成功');
    });
    res.onError((result) {
      AppToast.showWarning('提交失败');
    });
  }
}
